<?php defined('SYSPATH') or die('No direct script access.');

 class Controller_List extends Controller_Base {
      //
     public function action_index()
     {

        $cate=trim($this->request->param('id'));

        $category=array();

        $assort="";

        if(!empty($cate)){

            $category_model=new Model_Category();

            $category=$category_model->getInfoByUrl($cate);
            

            if(!empty($category)&&!empty($category['id'])){

                $assort=$category['id'];

            }

        }

        $pageSize=Kohana::$config->load('item.list')?Kohana::$config->load('item.list'):50;

        if($this->request-> is_ajax()){

            $page=intval($this->request->post("page"))?intval($this->request->post("page")):1;

        }
        else{

            $page=intval($this->request->query("page"))?intval($this->request->query("page")):1;

        }

        $page=$page>1?$page:1;

        $offset=$pageSize*($page-1);
        
        $sort=trim($this->request->query("s"));

        $time=trim($this->request->query("t"));

        $model=new Model_Post();

        $posts = Cache::instance()->get('list'."_".$assort."_".$time."_".$sort."_".$offset, null);

        if(empty($posts)){

            $posts=$model->getListInfo($offset,$pageSize,$assort,$sort,$time);

            Cache::instance()->set('list'."_".$assort."_".$time."_".$sort."_".$offset, $posts, 300);

        }

        if($this->request-> is_ajax()){

            foreach ($posts as $key => $value) {

                $posts[$key]['image']=Lib_Image::getPostImage($value['id'],$value['images'],260);
                
            }

            $result=array();

            $result['list']=$posts;

            exit(json_encode($result));

        }

        $total = Cache::instance()->get('list_items'.$assort.$time, null);

        if(empty($total)){

            $total=$model->getListItems($assort,$time);

        }

        $c_cache= Cache::instance();

        //获取分类信息
        $category_list= $c_cache->get('last_category',NULL);

        if(empty($category_list)){

            $assort_model=new Model_Assort();

            $category_list=$assort_model->getList();

            $c_cache->set('last_category',$category_list,3000000);
        }


        $param="";

        if(!empty($s)){

            $param.="?s=".$s;

            if(!empty($t)){

                $param.="&t=".$t;

            }
        }
        else{

            if(!empty($t)){

                $param.="?t=".$t;

            }
        }

        $url=Lib_Base::current_url();

        $view=View::factory('list/index');

        $view->page=$page;

        $view->url=$url;

        $view->param=$param;

        $view->cate=$cate;

        $view->assort=$assort;

        $view->sort=$sort;

        $view->time=$time;

        $view->posts=$posts;

        $view->pages=ceil($total/$pageSize);

        $view->total=$total;

        $view->category=$category;

        $view->category_list=$category_list;

        $this->template->content =$view;

        $this->template->title = __('Invent');

     }
 } 
